//
//  MYNewsShopDetailsBaseVC.m
//  MuYing
//
//  Created by tbb on 2020/3/5.
//  Copyright © 2020 TBB. All rights reserved.
//

#import "MYNewsShopDetailsBaseVC.h"
#import "MYDynamicCommentVC.h"
#import "MYNewsShopDetailsModel.h"
#import "MYImageBaseModel.h"
#import "MYDynamicDetailsGoodsCell.h"
#import "YQPInvitataionModel.h"
#import "MYNewsShopDetailsCommentVC.h"
#import "MYNewsShopDetailsHotelVC.h"
#define IMAGE_HEIGHT kYQPScreenWidth * (225/375.0)
#define NAVBAR_COLORCHANGE_POINT (IMAGE_HEIGHT - Height_NavBar*2)

@interface MYNewsShopDetailsBaseVC ()<MYDynamicDetailsGoodsCellDelegate>
@property (nonatomic, strong) UIButton * sharedBtn;

@property (nonatomic, strong) UIButton * carBtn;

@property (nonatomic, assign) CGFloat goodsCellH;
@property (nonatomic, strong) NSMutableArray * productArr;
@end

@implementation MYNewsShopDetailsBaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _setupNavView];
    //原点（0，64）开始
    self.edgesForExtendedLayout = UIRectEdgeTop;
    self.view.backgroundColor = UIColor.whiteColor;
    [self wr_setNavBarBackgroundAlpha:0.0];
    self.navigationController.navigationBar.translucent = YES;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = YES;
    //[[IQKeyboardManager sharedManager] setEnable:YES];
}

//保证进入下个界面nav正常显示
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = NO;
}

-(void)setupView {
    [super setupView];
    self.listTableView.backgroundColor = HexColor(0xFAFAFA);
    self.listTableView.frame = CGRectMake(0, 0, kYQPScreenWidth, kYQPScreenHeight-kHeight_Botton_SafeArea-49);
    
    [self.view addSubview:self.bottonView];
    [self.bottonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-kHeight_Botton_SafeArea);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(49);
    }];

    kYQREGISTERTABLEVIEWCHeaderFooter([MYShopDetailsHeaderView class])
    kYQREGISTERTABLEVIEWCELL([MYNewsDetailsHotelTitleCell class])
    kYQREGISTERTABLEVIEWNIBCELL(@"MYNewsShopDetailsTitleCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYNewsShopDetailsLocationCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYNewsShopDetailsTicketCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYNewsShopDetailsGoodsCell")
    kYQREGISTERTABLEVIEWCELL([MYShopDetailsImageCell class])
    kYQREGISTERTABLEVIEWCELL([MYNewsShopDetailsCategoryCell class])
    kYQREGISTERTABLEVIEWCELL([MYDynamicDetailsGoodsCell class])
    kYQREGISTERTABLEVIEWNIBCELL(@"MYNewsDetailsTimeCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYNewsDetailsLocationCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYShopDetailsCommentCell")
    kYQREGISTERTABLEVIEWCHeaderFooter([MYShopDetailsTitleHeaderView class])
    kYQREGISTERTABLEVIEWCHeaderFooter([MYShopDetailsCommentHeaderView class])
    kYQREGISTERTABLEVIEWCNIBHeaderFooter(@"MYNewsShopDetailsGoodsHeader")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYNewsGoodsCell")
    self.listTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getNewsData];
    }];
    [self getNewsData];
    YQSHOWLOADINGINVIEW;
}

-(void)getNewsData {
    NSString *url = [NSString stringWithFormat:@"%@%@",kGET_INFORMATION_RESERVATION_URL,self.informationId];
    if (self.isProduct ) {
        if (self.type == 0) {
            url = kGET_QUERY_HOTEL_INFO_URL(self.informationId);
        }else if(self.type == 1){
            url = kGET_QUERY_SCENERY_INFO_URL(self.informationId);
        }else{
            url = kGET_QUERY_CLASSES_INFO_URL(self.informationId);
        }
        
    }
    [YQPNetWorkingManager sendGetRequest:url parameters:nil success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        [self stopRefresh];
        if (responseObject.code == 0) {
            self.detailsModel = [MYNewsShopDetailsModel modelWithJSON:responseObject.data];
            MYNewsShopDetailsStandardModel *standardModel = self.detailsModel.standardList.firstObject;
            standardModel.isCurrent = YES;
            self.standarModel = standardModel;
            [self.productArr removeAllObjects];
            for (MYNewsShopDetailsProductModel *productModel in self.detailsModel.similarityProductList) {
                YQPSkuModel *skuModel = [YQPSkuModel new];
                skuModel.productName = productModel.name;
                skuModel.productId = productModel.Id;
                skuModel.price = productModel.priceScope;
                skuModel.defaultImage = productModel.defaultImage;
                skuModel.payNumber = productModel.payNumber;
                [self.productArr addObject:skuModel];
            }

            [self.listTableView reloadData];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQUITIPSERROR(error)
        YQHIDELOADINGTIPS
        [self stopRefresh];
    }];
    
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.section;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else if (section == 1) {
        return 2;
    }else if (section == self.section - 3){
        return self.detailsModel.commentList.count;
    }else if (section == self.section - 2){
        return self.detailsModel.detailsImage.count;
    }else if (section == self.section - 1){
        return 1;
    }else{
        if (self.section == 7) {
            if (section == 2) {
                return 1;
            } else {
                return self.standarModel.ticketList.count;
            }
        }else{
            if (self.detailsModel.standardList.count > 3) {
                return 3;
            }
            return self.detailsModel.standardList.count;
        }
    }
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        MYNewsShopDetailsTitleCell *titleCell = [tableView dequeueReusableCellWithIdentifier:@"MYNewsShopDetailsTitleCell"];
        titleCell.detailsModel = self.detailsModel;
        return titleCell;
    }else if (indexPath.section == 1) {
        MYNewsShopDetailsLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYNewsShopDetailsLocationCell"];
        cell.moreIcon.hidden = YES;
        if (indexPath.row == 0) {
            cell.icon.image = YQIMG(@"Login_Phone_select_icon");
            cell.textLb.text = [NSString stringWithFormat: @"%@: %@",kYQLS(@"联系电话"),self.detailsModel.phone];
        }else {
            cell.moreIcon.hidden = NO;
            cell.icon.image = YQIMG(@"OrderDetails_location_icon");
            cell.textLb.text = [NSString stringWithFormat:@"%@: %@",kYQLS(@"地址"),self.detailsModel.address];
        }
        return cell;
    }else if(indexPath.section == self.section - 3){
        MYShopDetailsCommentCell *commentCell = [tableView dequeueReusableCellWithIdentifier:@"MYShopDetailsCommentCell"];
        MYNewsShopDetailsCommentModel *commentModel = nil;
        if (indexPath.row < self.detailsModel.commentList.count) {
            commentModel = self.detailsModel.commentList[indexPath.row];
        }
        commentCell.commentModel = commentModel;
        return commentCell;
    }else if(indexPath.section == self.section - 2) {
        MYImageBaseModel *imageModel = nil;
        if (indexPath.row < self.detailsModel.detailsImage.count) {
            imageModel = self.detailsModel.detailsImage[indexPath.row];
        }
        MYShopDetailsImageCell *imageCell = [tableView dequeueReusableCellWithIdentifier:@"MYShopDetailsImageCell"];
        [imageCell.detailsIamgeView setImageWithURL:[NSURL URLWithString:imageModel.imageUrl] placeholder:YQIMG(@"")];
        return imageCell;
    }else if(indexPath.section == self.section - 1){
        
        MYDynamicDetailsGoodsCell *goodsCell = [tableView dequeueReusableCellWithIdentifier:@"MYDynamicDetailsGoodsCell"];

        goodsCell.collViewH = self.goodsCellH;
        goodsCell.delegate = self;
            
        goodsCell.goodArray = self.productArr.copy;
        
        return goodsCell;
    }else{
        if (self.section == 7) {
            if (indexPath.section == 2) {
                    MYNewsShopDetailsCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYNewsShopDetailsCategoryCell"];
                    cell.standardList = self.detailsModel.standardList;
                    ws(self);
                    cell.categorySelectedItemBlock = ^(NSInteger item) {
                        MYNewsShopDetailsStandardModel * selectedStandardModel = nil;
                        if (item < weakself.detailsModel.standardList.count) {
                            selectedStandardModel = weakself.detailsModel.standardList[item];
                            for (MYNewsShopDetailsStandardModel * standardModel in weakself.detailsModel.standardList) {
                                if (standardModel.standardId == selectedStandardModel.standardId) {
                                    standardModel.isCurrent = YES;
                                }else{
                                    standardModel.isCurrent = NO;
                                }
                            }
                            self.standarModel = selectedStandardModel;
                            [weakself.listTableView reloadData];
                        }
                    };
                    
                    return cell;
                }else {
                    MYNewsShopDetailsTicketCell *ticketCell = [tableView dequeueReusableCellWithIdentifier:@"MYNewsShopDetailsTicketCell"];
                    MYNewsShopDetailsStandardTicketModel *ticketModel = nil;
                    if (indexPath.row < self.standarModel.ticketList.count) {
                        ticketModel = self.standarModel.ticketList[indexPath.row];
                    }
                    ticketCell.ticketModel = ticketModel;
                    ws(self);
                    ticketCell.ticketBuyBlock = ^(MYNewsShopDetailsStandardTicketModel * ticketModel) {
                        [weakself reserveTicketModel:ticketModel];
                    };

                    return ticketCell;
                }
        }else{
            MYNewsShopDetailsGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYNewsShopDetailsGoodsCell"];
            MYNewsShopDetailsStandardModel * standardModel = nil;
            if (indexPath.row < self.detailsModel.standardList.count) {
                standardModel = self.detailsModel.standardList[indexPath.row];
            }
            cell.standardModel = standardModel;
            ws(self);
            cell.reserveActionBlock = ^(MYNewsShopDetailsStandardModel * ticketModel) {
                [weakself hotelGoToPayVC:ticketModel];
            };
            return cell;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 136;
    }else if (indexPath.section == 1) {
        return 44;
    }else if (indexPath.section == self.section - 3){
        MYNewsShopDetailsCommentModel *commentModel = nil;
        if (indexPath.row < self.detailsModel.commentList.count) {
            commentModel = self.detailsModel.commentList[indexPath.row];
        }
        return [tableView fd_heightForCellWithIdentifier:@"MYShopDetailsCommentCell" configuration:^(MYShopDetailsCommentCell *cell) {
            cell.commentModel = commentModel;
        }];
    }else if(indexPath.section == self.section - 2){
        MYImageBaseModel *imageModel = nil;
        if (indexPath.row < self.detailsModel.detailsImage.count) {
            imageModel = self.detailsModel.detailsImage[indexPath.row];
        }
        
        CGFloat h = 0.0;
        if (imageModel.wide>0.0 && imageModel.high > 0.0) {
            h = imageModel.high * kYQPScreenWidth / imageModel.wide;
            return h;
        }
        return kYQPScreenWidth;
    }else if(indexPath.section == self.section - 1){
        return self.goodsCellH;
    }else{
        if (self.section == 7) {
            //活动，景点
            if (indexPath.section == 2) {
                NSInteger row = 0.0;
                if (self.detailsModel.standardList.count % 3 == 0) {
                    row = self.detailsModel.standardList.count / 3;
                }else{
                    row = self.detailsModel.standardList.count / 3 + 1;
                }
                return row * 28 + row* 8 + 56;
            }else  {
                return 56;
            }
        }else{
            //酒店
            return 112;
        }
    }

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == self.section - 2) {
        
        NSMutableArray *urlArr = @[].mutableCopy;
        for (MYImageBaseModel *imageModel in self.detailsModel.detailsImage) {
            [urlArr addObject:imageModel.imageUrl];
        }

        MYShopDetailsImageCell *imageCell = [tableView cellForRowAtIndexPath:indexPath];
        
        YQBaseImageBrowserVC *imageBrowser = [[YQBaseImageBrowserVC alloc] initImageArrUrl:urlArr.copy currentIndex:indexPath.row sourceObject:imageCell];
        imageBrowser.currentPage = indexPath.row;
        [imageBrowser show];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        MYShopDetailsHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYShopDetailsHeaderView"];
        header.imageUrlStrArr = self.detailsModel.imageList;
        return header;
    }else if (section == 3 && self.section == 7){
        MYShopDetailsTitleHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYShopDetailsTitleHeaderView"];
        headerView.titleLb.text = kYQLS(@"选择门票种类:");
        headerView.titleLb.font = [UIFont systemFontOfSize:11 weight:(UIFontWeightMedium)];
        return headerView;
    }else if (section == self.section - 3){
        MYShopDetailsCommentHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYShopDetailsCommentHeaderView"];
        headerView.titleLb.text = kYQLS(@"用户评价");
        
        headerView.countLb.text = [NSString stringWithFormat:@"%@(%zd)",kYQLS(@""),self.detailsModel.goodNumber];
        ws(self);
        [headerView.moreBtn addTapAction:^(id  _Nonnull sender) {
            MYNewsShopDetailsCommentVC *commentVC = [MYNewsShopDetailsCommentVC new];
            commentVC.type = self.type;
            commentVC.informationId = self.informationId;
            [weakself.navigationController pushViewController:commentVC animated:true];
        }];
        return headerView;
    }else if (section == self.section - 2 || section == self.section - 1) {
        MYShopDetailsTitleHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYShopDetailsTitleHeaderView"];
        headerView.titleLb.font = [UIFont systemFontOfSize:15 weight:(UIFontWeightMedium)];
        if (section == self.section - 2) {
            headerView.titleLb.text = kYQLS(@"商品详情");
        }else{
            headerView.titleLb.text = kYQLS(@"相似产品");
        }
        
        return headerView;
    }else if(section == 2 && self.section == 6) {
        MYNewsShopDetailsGoodsHeader *goodsHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYNewsShopDetailsGoodsHeader"];
        [goodsHeader.selectDateBtn addTarget:self action:@selector(selectDateAction:) forControlEvents:(UIControlEventTouchUpInside)];

        NSInteger day = [NSDate pleaseInsertStarDate:self.data1 andInsertEndDate:self.data2].day;
        
        NSString *str1 = [NSDate getDateToString:self.data1 format:@"MM月dd日"];
        NSString *str2 = [NSDate getDateToString:self.data2 format:@"MM月dd日"];
        goodsHeader.titleLb.text = [NSString stringWithFormat:@"%@ %@(%zd%@)",str1,str2,day,kYQLS(@"晚")];
        return goodsHeader;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return kYQPScreenWidth * (225 / 375.0);
    }else if (section == 3 &&  self.section == 7) {
        return 28;
    }else if (section == self.section - 3) {
        return 84;
    }else if (section == self.section - 2 || section == self.section - 1) {
        return 48;
    }else if(section == 2 && self.section == 6) {
        return 45;
    }
    return 0.001;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    
    if (offsetY > NAVBAR_COLORCHANGE_POINT)
    {
        CGFloat alpha = (offsetY - NAVBAR_COLORCHANGE_POINT) / Height_NavBar;
        [self.leftItem setImage:YQIMG(@"nav_back_sIcon") forState:(UIControlStateNormal)];
        [self wr_setNavBarBackgroundAlpha:alpha];
        [self wr_setNavBarTintColor:[[UIColor blackColor] colorWithAlphaComponent:alpha]];
        [self wr_setNavBarTitleColor:[[UIColor blackColor] colorWithAlphaComponent:alpha]];
        self.title = kYQLS(@"详情");
    }
    else
    {
        [self wr_setNavBarBackgroundAlpha:0];
        [self.leftItem setImage:YQIMG(@"ShopDetails_back_icon") forState:(UIControlStateNormal)];
        [self wr_setNavBarTintColor:[UIColor whiteColor]];
        [self wr_setNavBarTitleColor:[UIColor whiteColor]];
        self.title = @"";
    }
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end
#pragma mark --MYDynamicDetailsGoodsCellDelegate
-(void)updateCollViewH:(CGFloat)h {
    self.goodsCellH = h;
    [self.listTableView reloadData];
}

- (void)didSelectItemAtItem:(NSInteger)item {
    YQPSkuModel *model = nil;
    if (item < self.productArr.count) {
        model = self.productArr[item];
    }
    MYNewsShopDetailsHotelVC *hotelVC = [MYNewsShopDetailsHotelVC new];
    hotelVC.type = self.type;
    hotelVC.informationId = model.productId;
    hotelVC.isProduct = YES;
    kYQPUSH(hotelVC);
    
}

#pragma --makr lazy

-(NSMutableArray *)productArr {
    if (!_productArr) {
        _productArr = [NSMutableArray array];
    }
    return _productArr;
}

-(MYShopDetailsBottonView *)bottonView {
    if (!_bottonView) {
        _bottonView = [MYShopDetailsBottonView viewFromXib];
        _bottonView.backgroundColor = UIColor.whiteColor;
        _bottonView.layer.shadowColor = HexColorA(0x000000, 0.6).CGColor;//RGB(70, 69, 69).CGColor;
        //2.设置阴影偏移范围
        _bottonView.layer.shadowOffset = CGSizeMake(0, 2);
        //3.设置阴影颜色的透明度
        _bottonView.layer.shadowOpacity = 0.5;
        
        //4.设置阴影半径
        _bottonView.layer.shadowRadius = 4;
        //5.设置阴影路径
        _bottonView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.navigationController.navigationBar.bounds].CGPath;
        
        _bottonView.addCar.hidden = YES;
        self.bottonView.likeBtn.hidden = YES;
        _bottonView.btnBgView.backgroundColor = UIColor.whiteColor;
        _bottonView.buyBtn.layer.cornerRadius = 16;
    }
    return _bottonView;
}
#pragma mark --lazy end
-(void)_setupNavView {
    [self.leftItem setImage:YQIMG(@"ShopDetails_back_icon") forState:(UIControlStateNormal)];
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 84, 30)];

//    UIButton *carBtn = [UIButton buttonWithType:0];
//    [carBtn setImage:YQIMG(@"ShopDetails_goodscar_icon") forState:(UIControlStateNormal)];
//    carBtn.frame = CGRectMake(0, 0, 30, 30);
//    self.carBtn = carBtn;
//    ws(self);
//    [self.carBtn addTapAction:^(id  _Nonnull sender) {
//        MYShopCarVC *carVC = [[MYShopCarVC alloc] init];
//        [weakself.navigationController pushViewController:carVC animated:true];
//    }];
//    [rightView addSubview:carBtn];
    
    UIButton *sharedBtn = [UIButton buttonWithType:0];
    [sharedBtn setImage:YQIMG(@"ShopDetails_shread_icon") forState:(UIControlStateNormal)];
    sharedBtn.frame = CGRectMake(54, 0, 30, 30);
    self.sharedBtn = sharedBtn;
    [rightView addSubview:self.sharedBtn];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightView];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}
@end
